package org.mall.modules.shop.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.mall.common.api.vo.CommonResult;
import org.mall.common.util.GeoHash;
import org.mall.common.util.ObjectUtils;
import org.mall.common.util.TimeUtils;
import org.mall.common.util.json.JacksonUtils;
import org.mall.convert.ShopConvert;
import org.mall.modules.order.entity.OrderDO;
import org.mall.modules.pojo.shop.ShopDTO;
import org.mall.modules.pojo.shop.ShopDetailDTO;
import org.mall.modules.shop.entity.Shop;
import org.mall.modules.shop.service.IShopService;
import org.mall.modules.shop.service.ShopService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 *@author yinxucun
 *@date 2019-11-21 11:34 
 */
@Slf4j
@Service
public class RpcShopServiceImpl implements ShopService {

    @Autowired
    IShopService shopService;

    @Override
    public CommonResult deleteAllRelationShip(String clerkId) {

        CommonResult commonResult=new CommonResult();
        try {
            shopService.deleteAllRelationShip(clerkId);

        }catch (Exception e){
            return CommonResult.error(500,"删除失败");
        }

        return CommonResult.success("删除成功");
    }

    @Override
    public CommonResult save(ShopDTO temp) {
        log.info(JacksonUtils.toJSONString(temp));
        Shop shop=new Shop();
        BeanUtils.copyProperties(temp,shop);
        try {
            shop.setCoordinate(GeoHash.encode(shop.getCoordinate()));
        } catch (Exception e) {
            return CommonResult.error("地址信息错误，报错失败");
        }
        log.info(JacksonUtils.toJSONString(shop));

        if (shopService.save(shop)){
            return CommonResult.success("保存成功");
        }
        else {
            return  CommonResult.error(500,"保存失败");
        }
    }

    @Override
    public CommonResult addSellerShopRelationShip(String id, String id1) {

        return CommonResult.success(shopService.addSellerShopRelationShip(id,id1));
    }

    @Override
    public CommonResult<List<ShopDTO>> getListOfStores(String userId) {

        return CommonResult.success(ObjectUtils.copyList(shopService.getListOfStores(userId), ShopDTO.class));
    }

    @Override
    public CommonResult<List<ShopDTO>> queryClerkBelongShops(String clerkId) {
        return CommonResult.success(ObjectUtils.copyList(shopService.queryClerkBelongShops(clerkId),ShopDTO.class));
    }

    @Override
    public CommonResult<List<ShopDTO>> getShopLisByClerkIds(List<String> clerkIds) {
        if (clerkIds==null||clerkIds.isEmpty()){
            return CommonResult.success(Collections.emptyList());
        }
        List<ShopDTO> list=shopService.getByClerkIds(clerkIds);

        return CommonResult.success(list);    }

    /**
     * 通过店铺id获取店铺的信息
     * @param shopIds
     * @return
     */
    @Override
    public CommonResult<List<ShopDTO>> getShopInfoById(List<String> shopIds) {
        if (shopIds==null||shopIds.isEmpty()){
            return CommonResult.error("参数不足");
        }
        return CommonResult.success(ObjectUtils.copyList(shopService.getById(shopIds),ShopDTO.class));
    }
    /**
     * 通过店铺id获取 一个 店铺的信息
     * @param shopId
     * @return
     */

    @Override
    public CommonResult<ShopDTO> getOneShopInfoByShopId(String shopId) {
       Shop shop=shopService.getById(shopId);
        if (shop!=null){
            ShopDTO shopDTO=new ShopDTO();
            BeanUtils.copyProperties(shop,shopDTO);
            return CommonResult.success(shopDTO);
        }
        return CommonResult.error("不存在");
    }

    @Override
    public List<ShopDetailDTO> getShopLisBySellerId(String userId) {
        List<ShopDetailDTO> list=shopService.getShopLisBySellerId(userId);
        return  list;
    }

    @Override
    public CommonResult addShopIncome(String shopId, Integer income) {
        return shopService.addShopIncome(shopId,income)?CommonResult.ok():CommonResult.error("添加失败");
    }

    @Override
    public Integer getShopCount() {
        return shopService.getAllShopCount();
    }

    @Override
    public Map<String, Integer> getYearAllShop() {
        Long yearStartTime = TimeUtils.getYearStartTime(System.currentTimeMillis(), "GMT+8:00");
        String timeStart = TimeUtils.timestampToStr(yearStartTime);
        Long yearEndTime = TimeUtils.getYearEndTime(System.currentTimeMillis(), "GMT+8:00");
        String timeEnt = TimeUtils.timestampToStr(yearEndTime);
        List<Shop> list= shopService.getAllYearAllShop(timeStart,timeEnt);
        Map<String, Integer> resultYearofMonth = new HashMap<>();
        SimpleDateFormat year = new SimpleDateFormat("MM");
        for (Shop month : list) {
            if (resultYearofMonth.containsKey(year.format(month.getCreateTime()))) {
                Integer monthPrices = resultYearofMonth.get(year.format(month.getCreateTime())) + 1;
                resultYearofMonth.put(year.format(month.getCreateTime()), monthPrices);
            } else {
                resultYearofMonth.put(year.format(month.getCreateTime()), 1);
            }
        }

        return resultYearofMonth;
    }

}
